﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace ceTerm
{
    public partial class Main : XForm, Util_PowerOn_NET.IUtil_PowerOn   //, ATScan_NET.IScan, AT_UHF_NET.IUHFHost
    {
        Point DefaultPageLocation = new Point(12, 3);
        public static string Password = "";
        public Main()
        {
            try
            {
                InitializeComponent();

                pnlSinyalGucu.Visible = false;
                this.WindowState = FormWindowState.Maximized;
                this.KeyPreview = true;
                Settings.Init();

                Terminal.Start(this);

                ceWS = new ceTerminal.ceTerminal.ceTerminal();

                string path = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
                string filePath = new System.IO.FileInfo(path).DirectoryName + @"\ceTerminal.txt";

                using (StreamReader sr = File.OpenText(filePath))
                    Password = sr.ReadToEnd().Trim();
                
                if (Password == "")
                {
                    using (ceTerminal.Password pass = new ceTerminal.Password())
                    {
                        pass.ShowDialog();
                        if (pass.DialogResult == DialogResult.OK)
                        {
                            if (pass.txtParola.Text == "")
                                throw new ApplicationException();

                            Password = pass.txtParola.Text;
                            using (StreamWriter sw = File.CreateText(filePath))
                                sw.WriteLine(Password);
                        }
                    }
                }
               

                ceWS.Url = Settings.WebServiceURL;

                pbEtiketleme.Click += new EventHandler(pb_Click);
                pbEtiketOkuma.Click += new EventHandler(pb_Click);
                pbTopArama.Click += new EventHandler(pb_Click);

                pbRaftanIndirme.Click += new EventHandler(pb_Click);
                pbIadeKabul.Click += new EventHandler(pb_Click);
                pbManualEtiketleme.Click += new EventHandler(pb_Click);

                pbStokKabul.Click += new EventHandler(pb_Click);
                pbGrupKontrol.Click += new EventHandler(pb_Click);


                page1.Location = page2.Location = DefaultPageLocation;
                page1.BringToFront();
            }
            catch (ApplicationException aex)
            {
                ShowException("Yetkisiz Kullanım!", "Hata", aex);
                this.Close();
            }
            catch (Exception ex)
            {
                ShowException("Web servisi hatası!", "Hata", ex);
                this.Close();
            }
        }

        private void frmMain_Closing(object sender, CancelEventArgs e)
        {
            Terminal.Stop();
        }

        //IUtil_PowerOn Members
        //PowerOn Notify Callback
        public void On_status_power()
        {
            // *** Must call this function. ***
            Terminal.RFID.PowerOnInit();
        }

        private void pb_Click(object sender, EventArgs e)
        {
            PictureBox button = (PictureBox)sender;

            if (button.Name == pbEtiketleme.Name)
                ShowForm(this, lblEtiketleme, new Etiketleme());
            else if (button.Name == pbEtiketOkuma.Name)
                ShowForm(this, lblEtiketOkuma, new EtiketOkuma());
            else if (button.Name == pbTopArama.Name)
                ShowForm(this, lblRaftanIndirme, new RaftanIndirme(RaftanIndirme.ListType.TopArama));
            else if (button.Name == pbRaftanIndirme.Name)
                ShowForm(this, lblRaftanIndirme, new RaftanIndirme(RaftanIndirme.ListType.RaftanIndirme));
            //MessageBox.Show("Yakında...", "ConfExpert", MessageBoxButtons.OK, MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button1);
            else if (button.Name == pbIadeKabul.Name)
                ShowForm(this, lblIadeKabul, new ListRfId());
            else if (button.Name == pbManualEtiketleme.Name)
                ShowForm(this, lblManualEtiketleme, new ManualEtiketleme());
            else if (button.Name == pbStokKabul.Name)
                ShowForm(this, lblStokKabul, new StokKabul());
            else if (button.Name == pbGrupKontrol.Name)
                ShowForm(this, lblGrupKontrol, new ReadRFID());

            //ShowForm(this, sender, new YerlesmeyenToplar());
            //ShowForm(this, sender, new Listeleme(Listeleme.ListelemeModu.CikisIptal));
            //ShowForm(this, sender, new EtiketlemeEski(EtiketlemeEski.EtiketlemeFormTipi.EtiketlenmeyenToplar));
            pbEtiketleme.Focus();
        }

        private void btnStokKabul_Click(object sender, EventArgs e)
        {
            ShowForm(this, lblManualEtiketleme, new StokKabul());
        }

        private void pbNext_Click(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            if (pb.Name == pbNext1.Name)
            {
                page2.Location = DefaultPageLocation;
                page2.BringToFront();
            }
            else if (pb.Name == pbNext2.Name)
            {
                page1.Location = DefaultPageLocation;
                page1.BringToFront();
            }
        }

        private void pbStokKabul_Click(object sender, EventArgs e)
        {

        }
    }
}
